package cn.yuanqiao.web.controller.archive.shuzihua;

import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.archive.appManager.service.ITenantMdataListService;
import cn.yuanqiao.archive.shuzihua.domain.YqCatalog;
import cn.yuanqiao.archive.shuzihua.domain.YqCatalogDis;
import cn.yuanqiao.archive.shuzihua.service.CatalogueImportService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 目录导入
 *
 * @ProjectName: cn.yuanqiao.archive.shuzihua.controller
 * @ClassName: CatalogueImportController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2024/1/16 10:39
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/archive/catalog/import")
public class CatalogueImportController extends BaseController {
    @Autowired
    private ITenantMdataListService mdataListService;

    @Autowired
    private CatalogueImportService catalogueImportService;

    /**
     * 获取表头
     *
     * @param categoryId
     * @param type
     * @param file
     * @return
     */
    @Log(title = "获取表头", businessType = BusinessType.IMPORT)
    @PostMapping("/getTitle")
    public AjaxResult getTitle(@RequestParam("categoryId") Long categoryId, @RequestParam("type") Integer type, MultipartFile file) {
        try {
            Integer itemtype = 0;
            if (type == 6) {
                type = 1;
                itemtype = 6;
            }
            JSONObject object = new JSONObject();
            object.put("xlsTitle", catalogueImportService.getTitle(categoryId, type, file).getHeadMap());  //表格表头
            TenantMdataList mdataList = new TenantMdataList();
            mdataList.setIsImport(1);
            mdataList.setType(type);
            List<TenantMdataList> tenantMdataLists = mdataListService.selectTenantMdataListListByCategoryId(mdataList, categoryId);
            if (itemtype == 6) {
                TenantMdataList  tenantMdataList = new TenantMdataList();
                tenantMdataList.setFieldName("iteam_code");
                tenantMdataList.setFieldCaption("项目编号(关联)");
                tenantMdataList.setShowType(4);
                tenantMdataList.setIsNoEmpty(1);
                tenantMdataLists.add(tenantMdataList);
            }
            object.put("sysTitle", tenantMdataLists);  //可导入字段
            return AjaxResult.success(object);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return AjaxResult.error();
    }

    /**
     * 档案导入
     *
     * @param dataStr
     * @param file
     * @return
     */
    @Log(title = "档案导入", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("dataStr") String dataStr, MultipartFile file) {
        logger.info(dataStr);
        try {
            catalogueImportService.upload(JSONObject.parseObject(dataStr), file);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
        return AjaxResult.success("导入成功");
    }

    /**
     * 查询日志列表
     *
     * @param yqCatalog
     * @return
     */
    @PostMapping("/log/list")
    public TableDataInfo logList(@RequestBody YqCatalog yqCatalog) {
        List<YqCatalog> yqCatalogs = catalogueImportService.logList(yqCatalog);
        return getDataTable(yqCatalogs);
    }

    /**
     * 查询日志详情
     *
     * @param id
     * @return
     */
    @PostMapping("/log/id")
    public TableDataInfo logList(@RequestParam("id") String id) {
        List<YqCatalogDis> dis = catalogueImportService.logById(id);
        return getDataTable(dis);
    }

    @Log(title = "目录导入详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestParam("id") String id) {
        List<YqCatalogDis> dis = catalogueImportService.logById(id);
        ExcelUtil<YqCatalogDis> util = new ExcelUtil<YqCatalogDis>(YqCatalogDis.class);
        util.exportExcel(response, dis, "目录导入详情数据");
    }
}
